{% load helpers %}
{% load static %}

{% if request.user.is_authenticated %}
    {% with is_apple_os=request.META.HTTP_USER_AGENT|split:'Mac OS X'|length|divisibleby:'2' %}
        <form action="{% url 'search' %}" class="col-4 text-center" id="header_search" method="get" role="search">
            <label class="position-relative w-100">
                <input
                    aria-placeholder="Press {% if is_apple_os %}⌘{% else %}Ctrl+{% endif %}K to search"
                    class="form-control nb-text-transparent"
                    name="q"
                    type="search"
                    value="{{ request.GET.q }}"
                />
                <span class="align-items-center d-inline-flex gap-6 overflow-hidden px-12 py-6 pe-none position-absolute start-0 top-0 user-select-none w-100">
                    <span aria-hidden="true" class="mdi mdi-magnify text-secondary d-inline-flex"></span>
                    {% if content_type %}
                        {% for tab_name, tab_details in nav_menu.tabs.items %}
                            {% for group_name, group_details in tab_details.groups.items %}
                                {% for item_link, item_details in group_details.items.items %}
                                    {% if item_details.is_active %}
                                        <span class="badge border" data-nb-link="{{ item_link }}"><!--
                                            -->in: {{ item_details.name }}<!--
                                            --><button tabindex="-1" type="button">
                                            <span aria-hidden="true" class="mdi mdi-close"></span>
                                            <span class="visually-hidden">Remove</span>
                                        </button>
                                        </span>
                                    {% endif %}
                                {% endfor %}
                            {% endfor %}
                        {% endfor %}
                    {% endif %}
                    <span
                        aria-hidden="true"
                        class="align-items-center d-inline-flex gap-4 text-nowrap{% if not request.GET.q %} text-secondary{% endif %}"
                    >
                        {% if not request.GET.q %}
                            Press <kbd class="mt-n1">{% if is_apple_os %}⌘{% else %}Ctrl+{% endif %}K</kbd> to search
                        {% else %}
                            {{ request.GET.q }}
                        {% endif %}
                    </span>
                    <a class="align-items-center d-inline-flex ms-auto pe-auto"
                       href="{% static 'docs/user-guide/platform-functionality/user-interface/search.html' %}"
                       target="_blank"
                       rel="noopener"
                    >
                        <span aria-hidden="true" class="mdi mdi-help-circle-outline text-secondary"></span>
                    </a>
                </span>
            </label>
        </form>
    {% endwith %}

    <div class="col-4 text-end">
        <div class="dropdown">
            <button class="btn dropdown-toggle align-items-center d-inline-flex gap-6" type="button" data-bs-toggle="dropdown" aria-expanded="false">
                <span aria-hidden="true" class="mdi mdi-account text-secondary"></span>
                {% firstof request.user.get_full_name request.user.username %}
            </button>
            <ul class="dropdown-menu dropdown-menu-end">
                <li><a class="dropdown-item" href="{% url 'about' %}"><span class="mdi mdi-information"></span>About Nautobot</a></li>
                <li><a class="dropdown-item" href="{% url 'user:profile' %}"><span class="mdi mdi-account-box"></span>Profile</a></li>
                <li><a class="dropdown-item" href="{% url 'extras:approver_dashboard' %}"><span class="mdi mdi-check-bold"></span>Approval Dashboard</a></li>
                {% if request.user.is_staff %}
                    <li><a class="dropdown-item" href="{% url 'worker_status' %}"><span class="mdi mdi-server-network"></span>Worker Status</a></li>
                    <li><a class="dropdown-item" href="{% url 'admin:index' %}"><span class="mdi mdi-cogs"></span>Admin</a></li>
                {% endif %}
                <li class="dropdown-divider"></li>
                <li><a class="dropdown-item" href="{% url 'logout' %}"><span class="mdi mdi-logout"></span>Log out</a></li>
            </ul>
        </div>
    </div>
{% else %}
    {% url settings.LOGIN_URL as login_url %}
    {% if request.path != login_url %}
        <div class="col-4 offset-4 text-end">
            <a class="btn btn-primary" href="{{ login_url }}?next={{ request.get_full_path | urlencode }}">
                <span aria-hidden="true" class="mdi mdi-login me-4"></span>Log in
            </a>
        </div>
    {% endif %}
{% endif %}
